/*
 * StatisticWriter.java
 *
 * Created on 28 de Abril de 2006, 17:04
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package hubsniffer.db;

import hubsniffer.DCHub;
import libjdc.dc.client.HubInfo;
import libjdc.dc.log.Log;
import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.text.MessageFormat;
import java.util.Iterator;

/**
 *
 * @author Daniel Botelho
 */
public class HubINFOWriter {
    
    private DataBaseManager db; 
    
    private void write_sql(String sql,boolean iso) throws UnsupportedEncodingException, SQLException{  
        db.writeRawSql(sql,iso);
    }
    
    /**
     *  Cria um Objecto do tipo StatisticWriter.
     *  � necess�rio chamar um m�todo public commit, para adicionar esta estatistica a BD 
     */
    public HubINFOWriter(DataBaseManager db) {
        this.db = db;
    }
    
    private Object fix(Object obj){
        return (obj==null)? null:"'"+obj.toString()+"'";
    }
    
    public void updateHubInfo(DCHub hub) throws SQLException, UnsupportedEncodingException{
        
        HubInfo hubInfo = hub.getHubInfo();
        if(hubInfo == null){
            System.out.println(hub.getHost()+"\t null");
            return;
        }else
            write_sql( "DELETE FROM hub_info WHERE hub_id =" + hub.getHub_id() ,true);
        
        String message = "insert into hub_info (\"hub_info_name\",\"hub_info_address\",\"hub_info_port\",\"hub_info_description\",\"hub_info_hubowner\",\"hub_info_max_users\",\"hub_info_min_share\",\"hub_info_min_slots\",\"hub_info_max_hubs\" ,\"hub_info_date\", \"hub_info_hub_type\",\"hub_id\")" +
                "VALUES ({0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11})";
        
        Object[] obj = {
            fix(hubInfo.getName()),
            fix(hubInfo.getAddress()),
            fix(hubInfo.getPort()),
            fix(hubInfo.getDescription()),
            fix(hubInfo.getHubowner()),
            fix(hubInfo.getMax_users()), 
            fix(hubInfo.getMin_share()), 
            fix(hubInfo.getMin_slots()),
            fix(hubInfo.getMax_hubs()),
            fix(hub.getHubSession().getStart()),
            fix(hubInfo.getHub_type()),
            hub.getHub_id()
        };
        
        //new MessageFormat(message);
        write_sql( new MessageFormat(message).format(obj),true);

    }
    /*
    public void updateHubInfo(Hub hub) throws SQLException, UnsupportedEncodingException{
        String message = "update hub set descricao=''{0}'',hub_min_share = {1}, hub_min_slots = {2}, hub_max_users = {3}, hub_max_hubs = {4} where hub_id = {5}";

        
        String descricao = Util.escapeString((hub.getHubDescription()==null)? "":hub.getHubDescription());
        String hub_min_share = (hub.getHubMinShare() == null)? "0":hub.getHubMinShare();
        String hub_min_slots = (hub.getHubMinSlots() == null)? "0":hub.getHubMinSlots();
        String hub_max_users = (hub.getHubMaxUsers() == null)? "0":hub.getHubMaxUsers();
        String hub_max_hubs = (hub.getHubMaxHubs() == null)? "0":hub.getHubMaxHubs();
        int hub_id = hub.getHub_id();
        
        Object[] obj = {descricao,hub_min_share,hub_min_slots,hub_max_users,hub_max_hubs,hub_id};
        
        //new MessageFormat(message);
        write_sql( new MessageFormat(message).format(obj),true);

    }*/
    
    public void commit( Iterator<DCHub> it) {
                
        while(it.hasNext()){
            try {
                updateHubInfo(it.next());
            } catch (SQLException ex) {
                ex.printStackTrace(Log.err);
            }catch (UnsupportedEncodingException ex) {
                ex.printStackTrace(Log.err);
            }
        }
        
    }


}
